package cms.manage.template;

import cms.bean.forumCode.ForumCodeReference;
import cms.bean.template.Forum;
import cms.bean.template.Layout;
import cms.service.template.TemplateService;
import cms.utils.threadLocal.TemplateThreadLocal;
import cms.manage.AccessSourceDeviceManage;
import cms.manage.template.value.TplValueFactoryStrategy;
import cms.manage.template.value.TplValueStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Size;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cms.common.SystemConstant.LayoutType.PUBLIC_REF_NEW_PAGE;
import static cms.common.SystemConstant.LayoutType.PUBLIC_REF_VALUE_PAGE;


/**
 * 模板管理入口
 * @author Administrator
 *
 */
@Slf4j
@Component("templateMain")
public class TemplateMain {
	@Resource TemplateService templateService;
	@Resource AccessSourceDeviceManage accessSourceDeviceManage;
	@Resource
	TplValueFactoryStrategy tplValueFactoryStrategy;
	/**
	 * 公共模板处理
	 * @param type 布局类型
	 * @param layoutFile 布局文件
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object> list(int type,String layoutFile,HttpServletRequest request){
		Map<String,Object> root = new HashMap<String,Object>();
		String dirName = templateService.findTemplateDirCache();//当前模板使用的目录

		List<Forum> list = templateService.findForumCache(dirName, type,layoutFile);
		String accessDevice = accessSourceDeviceManage.accessDevices(request);
		for(Forum forum : list){
			if(forum !=null && !root.keySet().contains(forum.getReferenceCode())){
				//结构   由|线作分割符 " 引用名称 | 引用路径"组成
				root.put(forum.getReferenceCode(), new ForumCodeReference(forum.getReferenceCode(),
						"templates/"+dirName+"/"+accessDevice+"/forum/"+forum.getModule()+".html"));
			}
		}
		return root;
	}
	/**
	 * 版块对应的模板对象
	 * @param forum
	 * @param submitParameter 提交参数
	 * @param runtimeParameter 运行时参数
	 * @return
	 */
	public Object templateObject(Forum forum,Map<String,Object> submitParameter,Map<String,Object> runtimeParameter){
		if(log.isInfoEnabled()){
			log.info("版块对应的模板对象form={}\nsubmitParameter={}\nruntimeParameter={}",forum, submitParameter,runtimeParameter);
		}
		TplValueStrategy tplValueStrategy = tplValueFactoryStrategy.getStrategy(forum.getForumType());
		if(null!=tplValueStrategy){
			return tplValueStrategy.doOperation(forum,submitParameter,runtimeParameter);
		}
		return null;
	}
	
	
	/**
	 * 公共页(引用版块值)[根据页面是否有标签智能调用]
	 * @param quoteTemplate 显示在某个页面上
	 */
	public Map<String,Object> publicQuoteCall(String quoteTemplate,HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		//当前模板使用的目录
		String dirName = templateService.findTemplateDirCache();
		Map<String, Layout> layoutMap = templateService.findLayoutCache(dirName, PUBLIC_REF_VALUE_PAGE.getValue());//公共页(引用版块值)
		if(MapUtils.isNotEmpty(layoutMap)) {
			String accessDevice = accessSourceDeviceManage.accessDevices(request);
			for (Layout layout : layoutMap.values()) {
				//每个引用代码只含有一个版块
				Forum forum = templateService.findForumCache(dirName, layout.getReferenceCode());
				if (forum != null) {
					//结构   由|线作分割符 " 引用名称 | 引用路径"组成
					map.put(forum.getReferenceCode(), new ForumCodeReference(forum.getReferenceCode() ,
							"templates/" + dirName + "/" + accessDevice + "/forum/" + forum.getModule() + ".html"));
				}
			}
		}
		return map;
	}

	/**
	 * 公共页(生成新引用页)[根据页面是否有标签智能调用]
	 * @param quoteTemplate 当前引用模板
	 * @throws Exception
	 */
	public Map<String,Object> newPublic(String quoteTemplate,HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		//当前模板使用的目录
		String dirName = templateService.findTemplateDirCache();
		//检查所有的公共页(生成新引用页)
		Map<String, Layout> publicLayoutMap  = templateService.findLayoutCache(dirName, PUBLIC_REF_NEW_PAGE.getValue());//公共页(生成新引用页)
		if(MapUtils.isNotEmpty(publicLayoutMap)) {
			String accessDevice = accessSourceDeviceManage.accessDevices(request);
			for (Layout layout : publicLayoutMap.values()) {
				map.put(layout.getReferenceCode(), new ForumCodeReference(layout.getReferenceCode() ,
						"templates/" + dirName + "/" + accessDevice + "/public/" + layout.getLayoutFile()));
				List<Forum> forumList = templateService.findForumByLayoutIdCache(dirName, layout.getId());
				if(CollectionUtils.isNotEmpty(forumList)) {
					for (Forum forum : forumList) {
						map.put(forum.getReferenceCode(), new ForumCodeReference(forum.getReferenceCode() ,
								"templates/" + dirName + "/" + accessSourceDeviceManage.accessDevices(request) + "/forum/" + forum.getModule() + ".html"));
					}
				}
				//添加到模板参数线程变量
				TemplateThreadLocal.setLayoutFile(layout.getLayoutFile());
			}
		}
		return map;
	}
}
